var clickButtonEl;
var clickedElId;

function openLayer(id, icon) {
	var offset = jQuery(icon).position();
	var layer = document.getElementById(id);
	
	//지원 호환 단말기 레이어 zIndex
	if (id=='mySupportedPhone' || id=='supportedPhone') {
		if (layer.style.zIndex < zTemp) layer.style.zIndex = zTemp+100;
	}

	//alert (layer.style.zIndex);
	if(icon) {
		layer.style.top = offset.top + "px";
		layer.style.left = (offset.left + 30) + "px";
		jQuery(layer).slideDown().mouseover(function(){
			closeLayer(id);
		});
	} else {
		layer.style.display = 'block';		
		zTemp = layer.style.zIndex;
	}
	setLayerFocus(id);
}
function closeLayer(id) {
	//$("#"+id).css('display','none');
	if(undefined != id){
		$("#"+id).attr("style", "display:none");
		$('body').css('overflow', 'visible'); // 팝업 스크롤 제어	
	}
	if(id == clickedElId && undefined != clickButtonEl){
		clickButtonEl.focus();
		clickButtonEl = undefined;
		clickedElId = "";
	}
}
function closeLayerFocus(){
	if(undefined != clickButtonEl){
		clickButtonEl.focus();
		clickButtonEl = undefined;
		clickedElId = "";
	}
}

function setLayerFocus(id, obj){
	if(undefined != id){
		var inputEl = document.getElementById(eval("\""+id+"\"")).getElementsByTagName("input");
		var selectEl = document.getElementById(eval("\""+id+"\"")).getElementsByTagName("select");
		var aEl = document.getElementById(eval("\""+id+"\"")).getElementsByTagName("a");
		
		//지원 호환 단말기 레이어 
		if (id=='mySupportedPhone' || id=='supportedPhone') {
			$(eval("\"#"+id+"\"")).find(".tab_list").focus();
		}else{
			if(inputEl.length>0){
				inputEl[0].focus();
				//console.log("input focus:"+inputEl[0].className);
			}else{
				if(selectEl.length>0){
					selectEl[0].focus();
					//console.log("select focus:"+selectEl[0].className);
				}else{
					if(aEl.length>0){
						aEl[0].focus();
						//console.log("a link focus:"+aEl[0].href);
					}
				}
			}
		}
		clickedElId = id;
	}
	if(undefined != obj){
		//console.log(obj.className);
		clickButtonEl = obj;
	}
}

function setClickButtonEl(obj){
	clickButtonEl = obj;
}

function focusMove(id){
	$(id).focus();
}

/**
 * ixDevice 디바이스 선택버튼
 * @package	{ixDevice}
 * @version 20130805 (jslee)
*/
if ( ixDevice ) throw new Error('[ixError] "ixDevice"가 이미 존재하여 충돌이 발생!');
var ixDevice = ( function ( $container ) {	
	
	var tabAnchor = $container.find('> ul li .tab');
	tabAnchor.each(function(){
		var $menu = $(this);
		var $targetEl = $('#'+$menu.attr('href').split('#')[1]);
		
		$targetEl.css('display', 'none');
		if ($menu.hasClass('on')) {
			$targetEl.css('display', 'block');
		};

		$menu.click(function(){				
			if (!$(this).hasClass('on')) {
				tabAnchor.removeClass('on');
				tabAnchor.each(function(){
					$($(this).attr('href')).css('display', 'none');
				});
				$menu.addClass('on');
				$targetEl.css('display', 'block');
			};
			return false;
		});	
	});
});

/**
 * ixScroll - top버튼
 * @package	{ixScroll}
 * @version 20130805 (jslee)
*/
if ( ixScroll ) throw new Error('[ixError] "ixScroll"가 이미 존재하여 충돌이 발생!');
var ixScroll = ( function () {	
	var scrollBtn = $( '#top-scroll' );	
	moveTopbtn()
	$( window ).on ('resize' , function() {
		moveTopbtn()
	})
	$( window ).scroll( moveTopbtn );
	function moveTopbtn(){
		var _scrollPos = $( window ).scrollTop();		
		if ($( window ).height() + _scrollPos < 690) {
			scrollBtn.css({ 'position':'', 'top':'690px' });					
		}else if (  $( window ).height() + _scrollPos < $( document).height() - $( '#ncomui-footer' ).height() ){
			scrollBtn.css({ 'position':'fixed', 'bottom':'0px' });
		}else{
			scrollBtn.css({ 'position':'', 'bottom':'' });					
		}
	}
});

(function($) {
    $.fn.uChSelect = function(options, id){
        return this.each(function(){
            var S = $.extend({cName:'cmselw',onChange:null},options);
            var $select = $(this);
            var style = $select.attr('style');
            if(style != null){
                style = style.replace("inline","");
            }
            var $wrap = $select.wrap('<div />').parent();
                $wrap.addClass('cmSelD '+ S.cName +' ' + $select.attr('className')).attr({'style':style})
                    .prepend('<div class="t-top"><span class="tit"></span><span class="arr"></span></div><div class="a-lay"><div class="scroll"><ul></ul></div></div>');
                $select.hide();

            var $list = $wrap.find('.a-lay').hide();
            var $scroll = $wrap.find('.scroll');

            var wid = $select.outerWidth(true) + 10;
            $wrap.css({width:wid}).bind('mouseleave',function(){
                var $self = $(this);
                $list.slideUp('fast',function(){ $self.css({zIndex:''});   });
            });
            $scroll.width(wid-2);
            if($scroll.height() >0 && $.browser.msie && $.browser.version==8) $scroll.width(wid-3).css('paddingRight','0.05em');

        	$select.find('option').each(function(i){
                var target =($(this).attr('target'))? 'target="'+$(this).attr('target') +'"' : '';
                if(options != null){
                	if(options[i] =='X' && ($select.attr('id')=='devicesSelect' || $select.attr('id')=='deviceSel')){
						$list.find('ul').append($('<li class=\"nosel\"><a '+target+' index="'+ i +'" class=\"popnosel\">'+ $(this).html() +'</a></li>'));
					}else if(options[i] =='N'){
						$list.find('ul').append($('<li class=\"nosel\"><a '+target+' index="'+ i +'" class=\"nosel\">'+ $(this).html() +'</a></li>'));
					}else{
						$list.find('ul').append($('<li><a href="#"'+target+' index="'+ i +'">'+ $(this).html() +'</a></li>'));
					}
                } else {
                 $list.find('ul').append($('<li><a href="#"'+target+' index="'+ i +'">'+ $(this).html() +'</a></li>'));
                }
            });

            $list.find('a').click(function(){
            	if($(this).attr('class') != 'popnosel'){
            		var btnClass;
            		if($(this).attr('class') == 'nosel'){
            			btnClass = "btn-gray";
            		}else if ($(this).attr('class') == 'nosel selected'){
            			btnClass = "btn-gray";
            		} else {
            			btnClass = "btn-blue";
            			
            		}
            		$(id).attr("class", btnClass);
//            		$('#btn_purchase').attr("class", btnClass);
            		
	            	$('a.selected', $wrap).removeClass('selected');
	                $(this).addClass('selected');
	                if ($select[0].selectedIndex != $(this).attr('index')){
	                     $select[0].selectedIndex = $(this).attr('index');
	                     $select[0].target = $(this).attr('target');
	                    if($select[0].onchange) $select[0].onchange();
	                    if(S.onChange) S.onChange();
	                }
	                $('.tit', $wrap).html($(this).text());
            	}
                $list.hide();
                return false;
            });

            $('a:eq('+ $select[0].selectedIndex +')', $list).trigger('click');

            //if($select.attr("disabled")) $wrap.addClass('cmSelDisabled');
            $(".t-top", $wrap).click(function(){
                $wrap.css({zIndex:9997});
                if ($list.css('display')=='none'){ $('.cmSelD .A:visible').hide(); };

                // maverick에서 조건절 추가(disalbed처리를 위한 조건)
                if($select.attr("disabled") != true){
                    $list.slideToggle('fast', function(){ //$list.toggle()
                        var offSet = ($('a.selected', $list).offset().top - $list.find("ul").offset().top);
                        $scroll.animate({scrollTop: offSet});
                    });
                    $wrap.removeClass('cmseldisabled');
                }else{
                     $wrap.addClass('cmseldisabled');
                    //disalbed 했을 때 wapper에 disabled된 표시로 css를 입혀야 함.
                }
                return false;
            })
        });
    };

    /*
      * selectbox의 wapper를 리로드하기 위해 추가
      * */
    $.fn.uChSelectReload = function(opt){
        var S = $.extend({cName:'cmSelW',onChange:null},opt);
        var $cmSel = $(this).parent();
        var select = $cmSel.find('select');

        if($cmSel != null){
            $cmSel.replaceWith(select);
        }
        $(this).show().uChSelect(opt);
    };

	$.fn.uChCheckedReload = function(opt){
             var S = $.extend({chkW :"cmCheck", radW:"cmRadio", onChange:null, onTag:''}, opt);
            var $this = $(this);
            var self = $(this);
            var type = $this.attr("type");
            var cName = (type=="checkbox")? S.chkW : S.radW;
            var cNameOn = cName+"_on";
             var par = $this.parent();

             if(type == "checkbox") {
                             $this.attr({"checked": $this.is(":checked")? "" : "checked"});
                            self.is(':checked')? par.addClass(cNameOn) : par.removeClass(cNameOn);
                            if(S.onTag) self.is(':checked')? self.parents(S.onTag).addClass('on') :  self.parents(S.onTag).removeClass('on');
                }
                if(type == 'radio') {
                             $this.attr({"checked": $this.is(":checked")? "" : "checked"});
                            self.is(':checked')? par.addClass(cNameOn) : par.removeClass(cNameOn);
                            if(S.onTag) self.is(':checked')? self.parents(S.onTag).addClass('on') :  self.parents(S.onTag).removeClass('on');
                }

    };

     $.fn.uChForm = function(options) {
   // S= $.extend({chkW :"cmChkT1", radW:"cmChkT", hide:"cmHide", noChange:0}, options);
        return this.each(function() {
            var S = $.extend({chkW :"cmCheck", radW:"cmRadio", onChange:null, onTag:''}, options);
            var $this = $(this);
            var type = $this.attr("type");
            var cName = (type=="checkbox")? S.chkW : S.radW;
            var cNameOn = cName+"_on";

            $this.css({display:'none'}).wrap('<span class="'+cName+' "/>');
            var par = $this.parent();
            makeLabel($this);

            par.click(function(){
                if(type == "checkbox") {
                    $this.attr({"checked": $this.is(":checked")? "" : "checked"});
                }

                if(type == "radio"){ $this.attr({"checked": "checked"}); };

                $this.trigger("change");
                return false;
            });

            $this.change(function() {
                var self = $this;
                if(type == "checkbox") {
                    self.is(':checked')? par.addClass(cNameOn) : par.removeClass(cNameOn);
                    if(S.onTag) self.is(':checked')? self.parents(S.onTag).addClass('on') :  self.parents(S.onTag).removeClass('on');
                }
                if(type == 'radio') {
                    $('input[name="'+$this.attr('name')+'"]').parent().removeClass(cNameOn);
                    if(self.is(':checked')){
                        par.addClass(cNameOn);
                        if(S.onTag) {
                            $('input[name="'+$this.attr('name')+'"]').parents(S.onTag).removeClass('on');
                            self.parents(S.onTag).addClass('on');
                        }
                    }
                }
              //  makeLabelClass($this);
                if(S.onChange) S.onChange(self);
            });

            if($this.is(':checked')) {
                par.addClass(cNameOn);
                if(S.onTag) par.parents(S.onTag).addClass('on');
               // makeLabelClass($this);
            }
        });

        function makeLabelClass(el){
            var label = el.parent().next() || el.parent().prev();
            if(label) el.is(':checked')? label.addClass('on') : label.removeClass('on');
        }

        function makeLabel(el){
            var label = el.parent().next() || el.parent().prev();
            if(label.is("label")) label.css('cursor','pointer').addClass('cmLabel').click(function(){el.parent().trigger('click'); return false;});
        }
    };

    $.fn.uCheckAll = function(name, onChange) {
        return this.each(function() {
            var $this = $(this);
            $this.change(function() {
                var target = $('input[name='+name+']')
                target.each(function(){
                    $this.is(":checked")? $(this).attr({"checked":"checked"}) : $(this).removeAttr('checked');
                    $(this).trigger('change');
                });
                if(onChange) onChange($this);
                return false;
            });
        });
    };

    $.fn.uCheckNoun = function(name){// 2012-02-15 추가 함수 : 인풋 체크시 전체선택 해제

    	var target = $(name).find('input');
		var $this = $(this);
		$this.parents().click(function(){
			var cekIn = $(this).find('input').attr('checked');
			if(!cekIn){
				target.removeAttr('checked').parents('.cmCheck').removeClass('cmCheck_on');
			};
		});

	};

    $.fn.uChFile = function(options) {
        var S = $.extend({ btnH :'19', btnW :'51'},options);
        return this.each(function() {
            var $this = $(this);
            var wid =  $this.width() ;

            var wrapper = $('<div class="cmFileD" />').addClass($this.attr("class")).attr({'style':$this.attr('style')}).css({width:'auto'})
            var filename = $('<input readonly  />').css({width:wid});
            var btn = $('<span class="cmFileBtn" />').css({ width:S.btnW, height:S.btnH});
            $.browser.mozilla ? $this.css({marginLeft:-142, width:0}) : $this.css({marginLeft:S.btnW - wid  });
            $this.wrap(wrapper).before(filename).wrap(btn).hide();
            setTimeout(function(){$this.show()},700)
            //$this.bind("change", function() {
            //    filename.val($this.val());
            //});
        });
    };

    $.fn.uChFormAll = function(options, id){
        return this.each(function() {
        	var elementId = id;
            var el = $(this);
            if(el.is("select")){
              if(el.attr("multiple") != true){
                if(el.attr("size") == undefined || el.attr("size") <= 1){
                    el.uChSelect(options, elementId);
                }
              }
            }else if(el.is(":radio")){
              el.uChForm(options);
            }else if(el.is(":file")){
              el.uChFile(options);
            }
        });
    };


    $.fn.uShowHide = function(options){
        return this.each(function() {
            var S = $.extend({event:'mouseenter', target:null, handle:null, isOverOut:null},options);
            var $this = $(this);
            var $handle = (S.handle != null)? $(this).find(S.handle) : $this;
            var vTarget = S.target;
            var vIsOverOut = S.isOverOut;

            $handle.bind(S.event, function(){
                if($this.has(vTarget)) $this.find(vTarget).show();
                if(S.event == 'click') return false;
            })

            $this.mouseleave(function(){
                if($(this).has(vTarget+':visible')) $(this).find(vTarget).hide();
                if(S.event == 'click') return false;
            })

            if(vIsOverOut != null ) $(vIsOverOut).uOverOut();
        });
    };

    $.fn.uOverOut = function(options){
        var S = $.extend({cName:'on'},options)
        $(this).mouseenter(function(){$(this).addClass(S.cName);})
               .mouseleave(function(){$(this).removeClass(S.cName);})
    };

   $.fn.uStatusRollOver= function(options){
        var S = $.extend({cName:'on'},options)
        var $this = $(this);
        $this.bind('mouseenter mouseleave', function(){
            $this.css({zIndex:0});
            $(this).css({zIndex:1}).toggleClass(S.cName);
        });
    };
})(jQuery);

// Toggle 2013-11-04

$(function(){   
	   $("#list2 .btn-close01").hide();   
	   $("#list2 .btn-view01").click(          
		   function(){
				var titleText = $(this).parents().prev().prev().prev().children(".apps-name a")

				$(this).parents().next("tr").show(); 
				$(this).parents("tr").addClass("on");
				$(titleText).css("color","#3168A0");
				$(this).hide(); 
				$(this).next(".btn-close01").show(); 
				
				$(this).children(".item .close01").show();
				$(this).children(".item .vew").hide();	
				$(this).children(".item .view").hide();	
				return false;
		   });
	   $("#list2 .btn-close01").click(function(){
				var titleText = $(this).parents().prev().prev().prev().children(".apps-name a")

			    $(this).parents().next("tr").hide();
				$(this).parents("tr").removeClass("on");
				$(titleText).css("color","#010101");
				$(this).hide(); 
				$(this).prev(".btn-view01").show(); 
				
				$(this).children(".item .close01").hide();
				$(this).children(".item .vew").show();
				$(this).children(".item .view").show();
				
				return false;
				
	       });
	});







